#include "DayTimeAndPosLampView.h"
#include "F3LampInterfaces.h"

static void DaytimeAndPosnCanSignalFeedback(DayTimeAndPosLampView* this_)
{
    float daytimeOrPosnLampBri = this_->model_->pwm_;
       if(daytimeOrPosnLampBri > 45.0)
       {
           Set_CANSIG_FLDaytimeLampOnSt(OnOff_On);
           Set_CANSIG_LeFrntPosnLampOnSt(OnOff_Off);
           Set_CANSIG_LeFrntPosnLampBri(daytimeOrPosnLampBri);    
       }
       else if(daytimeOrPosnLampBri <= 45.0 && daytimeOrPosnLampBri > 0.0)
       {
           Set_CANSIG_FLDaytimeLampOnSt(OnOff_Off);
           Set_CANSIG_LeFrntPosnLampOnSt(OnOff_On);
           Set_CANSIG_LeFrntPosnLampBri(daytimeOrPosnLampBri);
       }
       else
       {
            if(this_->model_->onOffStatus_ == OnOff_Invalid)
            {    
                Set_CANSIG_FLDaytimeLampOnSt(OnOff_Invalid);
                Set_CANSIG_LeFrntPosnLampOnSt(OnOff_Invalid);
            }
            else
            {
                Set_CANSIG_FLDaytimeLampOnSt(OnOff_Off);
                Set_CANSIG_LeFrntPosnLampOnSt(OnOff_Off);
            }
            Set_CANSIG_LeFrntPosnLampBri(daytimeOrPosnLampBri);
       }
}

float calculateDaytimeDerateCoefficient()
{
    float coefficient = 100.0f;

    do
    {
        if(IsDaytimeAndPosnNTCOpenCircuit())
        {
            //Ignore
            break;
        }

        if(IsDaytimeAndPosnNTCShortCircuit())
        {
            //Ignore
            break;
        }

        coefficient = LocalQuery_LobeamBrightness_Temperature();

    } while (false);
    
    
    float coefficient_v = LocalQuery_LobeamBrightness_Voltage();
    coefficient = coefficient < coefficient_v ? coefficient : coefficient_v;

    return coefficient;
}

void execute_TimeLampView(DayTimeAndPosLampView* this_)
{
    DaytimeAndPosnCanSignalFeedback(this_);

    uint32_t mask = !IsFloatEqual(this_->model_->pwm_, this_->lastModel_.pwm_) ||
                this_->model_->mask_ != this_->lastModel_.mask_  ? 0xffffffff : 0x00000000;

    if(mask)
    {
        float coefficient = calculateDaytimeDerateCoefficient();
        if(coefficient > 0.0)
        {
            Set_LampCtrl_DaytimeAndPosnLampsStatus(mask, this_->model_->mask_, this_->model_->pwm_ * coefficient / 100.0f);
        }
        else
        {
            Set_LampCtrl_DaytimeAndPosnLampsStatus(mask, 0, 0);
        } 
    }         
}

float calculateStarrySkDerateCoefficient()
{
    float coefficient = 100.0f;

    do
    {
        if(IsLoBeamNTCOpenCircuit())
        {
            //Ignore
            break;
        }

        if(IsStarrySkyNTCOpenCircuit() || IsHiBeamNTCOpenCircuit())
        {
            coefficient = LocalQuery_StarrySkyLampBrightness_Temperature();
        }

    } while (false);
    

    float coefficient_v = LocalQuery_StarrySkyLampBrightness_Voltage();
    coefficient = coefficient < coefficient_v ? coefficient : coefficient_v;

    return coefficient;
}

void execute_StarrySkyView(StarrySkyView* this_)
{
    uint32_t mask = this_->model_->mask_ ^ this_->lastModel_.mask_;
    if(!IsFloatEqual(this_->model_->pwm_, this_->lastModel_.pwm_))
    {
        mask = 0x3ffff;
    }

    float coefficient = calculateStarrySkDerateCoefficient();
    if(coefficient > 0.0)
    {
        Set_LampCtrl_StarrySkyLampsStatus(mask, this_->model_->mask_, this_->model_->pwm_ * coefficient / 100.0f);
    }
    else
    {
        Set_LampCtrl_StarrySkyLampsStatus(mask, 0, 0.0);
    }
}
